package com.tz.service.impl;

import com.tz.entity.User;
import com.tz.entity.search.UserSearchPojo;
import com.tz.mapper.UserMapper;
import com.tz.service.UserService;
import com.tz.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Alex
 * @date 18-1-2
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public PageBean<User> queryUserByPage(Integer pageNow, Integer pageSize, UserSearchPojo userSearchPojo) {
        PageBean<User> pb=new PageBean<>();

        pb.setPageSize(pageSize);
        pb.setPageNow(pageNow);
        pb.setTotalRecords(userMapper.countUser(userSearchPojo));
        pb.setPageDatas(userMapper.selectUserByPage(pageNow,pageSize,userSearchPojo));
        return pb;
    }

    /**
     * 只可以选择启用的用户
     * @return 所有启用的用户
     */
    @Override
    public List<User> findAll() {
        return userMapper.selectAll(1);
    }

    @Override
    public List<User> findAllByRole(Long roleid) {
        return userMapper.selectAllByRoleid(1,roleid);
    }

    @Override
    public void changeBatchUserStatus(List<Long> ids, Integer enable) {
        userMapper.changeBatchUserStatus(ids, enable);
    }

    @Override
    public User login(String username, String password) {
        return userMapper.selectUserByPassword(username,password);
    }

    @Override
    public void changeUser(User user) {
         userMapper.updateUser(user);
    }

    @Override
    public User viewUserById(Long id) {
        return userMapper.selectUserById(id);
    }

    @Override
    public void addUser(User user) {
        userMapper.insertUser(user);
    }

    @Override
    public void resetPassword(List<Long> ids) {
        userMapper.changeDefPassword(ids);
    }
}
